package training_set;
import java.io.*;
import java.util.*;
import preprocess.*;
import pairwise_model.*;
public class Global_Positive_Set {
	public List<Pair> Extract_Positive_Pairs(List <ArticleUnit> articles, String xml_file, String authority_file, String stop_mesh, String cui_profile, String semantics_profile){
		//List <ArticleUnit> articles = new One_Instance_Filter().XMLParser(xml_file);
		pairwise_name Names = new pairwise_name(authority_file);
		List<ArticleUnit> candidates = new ArrayList<ArticleUnit>();
		List<Pair> global_pairs = new ArrayList<Pair>();
		for(ArticleUnit article: articles){
			String pmid = article.getPmid();
			if(Names.authority.containsKey(pmid))
				candidates.add(article);
		}
		pairwise_mesh pm = new pairwise_mesh(stop_mesh);
		Pairwise_CUI pcui = new Pairwise_CUI(cui_profile);
		Pairwise_CUI psemantics = new Pairwise_CUI(semantics_profile);
		for(int i=0; i<candidates.size(); i++)
			for(int j=i+1; j<candidates.size(); j++){
				int name_score = Names.Score_from_Authority(candidates.get(i).getPmid(), candidates.get(j).getPmid());
				if(name_score<=0)
					continue;
				int journal_score = Get_Journal_Score(candidates.get(i).getJournal(), candidates.get(j).getJournal());
				int mesh_score = pm.CountCommon(candidates.get(i).getMesh(), candidates.get(j).getMesh());
				if(journal_score>0 || mesh_score>=3){
					// write the information of this pair down.
					int majormesh_score = pm.CountCommon(candidates.get(i).getMajorMesh(), candidates.get(j).getMajorMesh());
					double coldsentence_cosine = new Pairwise_Cosine().BuildProfile(candidates.get(i).getCold_Sentence(), candidates.get(j).getCold_Sentence());
					int cui_score = pcui.Common_CUI_Score(candidates.get(i).getPmid(), candidates.get(i).getPmid());
					int semantics_score = psemantics.Common_CUI_Score(candidates.get(i).getPmid(), candidates.get(j).getPmid()); 
					int bigram_score = new Pairwise_Bigram().CommonBigram(candidates.get(i).getBigram_profile(), candidates.get(j).getBigram_profile());
					Pair p = new Pair(candidates.get(i).getPmid(), candidates.get(j).getPmid(),name_score,mesh_score, majormesh_score, 
										journal_score, coldsentence_cosine, cui_score, semantics_score, bigram_score);
					global_pairs.add(p);
				}
					
			}
		return global_pairs;
	}
	
	public int Get_Journal_Score(String journal1, String journal2){
		int score = 0;
		if(journal1 == null || journal2 == null)
			return score;
		journal1 = journal1.trim();
		journal2 = journal2.trim();
		if(journal1.equalsIgnoreCase(journal2))
			return 1;
		return score;
	}
}
